<%-- 
    Document   : VerifyFile
    Created on : Oct 26, 2014, 12:44:20 PM
    Author     : user
--%>

<%@page import="newpackage.CountWord"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="newpackage.CompareFileContents"%>
<%@page import="newpackage.compareFileContent"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="newpackage.ConnectionFactory"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
<script>
    
function myFunction(name) 
{
 //   var secret_key = prompt("Please enter your secret key", "Please enter your secret key");
  //  var block = prompt("Please enter block name", "Block no");
  //  if (secret_key != null && block!=null) 
 //   {
 document.getElementById("file_name").value=name;
           document.forms["verify_file"].submit();
   // }
}
</script>
</head>
<body background="image.jpg">
    <h1> ENSURING PRIVACY AND DATA FRESHNESS OF SHARED DATA IN CLOUD </h1>
    <center>
    <form id="verify_file" name="verify_file">
    <%! ArrayList<String> getFileNames()throws Exception
         {
         ArrayList<String>  names=new ArrayList<String>();
           Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.filename FROM file_details ";
           ResultSet rs=select.executeQuery(Query);
           int i=0;
           while(rs.next())
           {
               String s=rs.getString(1);
               names.add(s);
           }
           return names;
        }
      %>
      
       <%! void storeAuditTime(String file,long duration)throws Exception
         {
          String query="insert into Audit_time(file_name,execution_time) values ('"+file+"','"+duration+"')";
          Connection con=ConnectionFactory.getConnection();
          Statement insert=con.createStatement();
          insert.executeUpdate(query);
          
        }
      %>   
      
      
      
      
      
     <%
     ArrayList<String> files=getFileNames();
        %>
        <p>
            Select file Name:
        </p>
        
        <select name="file_name" onchange="myFunction(this.value)" id="file_name">
            <option>select file</option>
        <%
        if(request.getParameter("file_name")!=null)
        {
    %>
        <option value="<%=request.getParameter("file_name")%>" selected="" ><%=request.getParameter("file_name").substring(request.getParameter("file_name").lastIndexOf('/')+1)%></option>
          <%  
        }
        for(String name:files)
        {
        if(name.equals(request.getParameter("file_name")))
        continue;
        %>
    <option value="<%=name%>" ><%=name.substring(name.lastIndexOf('/')+1)%></option>
        <%
        }
        %>
        </select>
        
        <input type="hidden" name="block_name" id="block_name"/>
        <input type="hidden" name="secret_key" id="secret_key"/>
        <%
        String block_name=request.getParameter("block_name");
        String secret_key=request.getParameter("secret_key");
        String file_name=request.getParameter("file_name");
        boolean compare=false;
        if(block_name!=null&&secret_key!=null)
        {
          //  compare=compareFileContent.compare(file_name, file_name.replace("main","sub"));
          //  compare=CompareFileContents.contentEquals(new FileInputStream(file_name),new FileInputStream(file_name.replace("main","sub")));
        long startTime=System.nanoTime();

        int c1=CountWord.getCount(file_name);
        int c2=CountWord.getCount(file_name.replace("main","sub"));
        if(c1==c2)
        {
           compare=true;
        }
        if(compare)
            {
             out.print("File is safe");
            }
            else
               {
                out.println("<input type='hidden' name='file_name' value='"+file_name+"'/>"); 
                out.print("File is Modified");
               }
        long stopTime=System.nanoTime();
        long duration=stopTime-startTime;
        
        storeAuditTime(file_name.substring(file_name.lastIndexOf('/') + 1),duration);
        
         }
        
        
%>


<input type="submit" formmethod="post" formaction="Update.jsp"  value="Update"/>
<input type="submit" formmethod="post" formaction="Revert.jsp"  value="Revert"/>
<a href="FileUpload.jsp">Back</a>
<a href="Login.jsp">LogOut</a>
    </form>
</center>
</body>
</html>
